document.write('<script src="helper.js"></script>');
function testProject(){
    	testListProjects();
    	testAddProject();
    	testRenameProject();
    	testListReleases();
    	testGetUsers();
    	testAssignUser();
        testDissociateUsers();
    	testDelProject();
	testAddRelease();
	testDelRelease();
       	testListStickies();
}
function testDissociateUsers(){
        helperLogout();
        helperLogin(true);
	write("Testing DissociateUsers");
	// first add project, associate users with that project, then test
	//first associate users
	var name = {"projName" : "test project"};
	var addRet = get("projectApi.php", "addProject", name);
	
	var assignInfo = { "users" : [2], "projectId" : addRet.projectId};
	//trying to dissociate a user from a project he wasn't associated with
	var ret = get("projectApi.php","dissociateUsers",assignInfo);
	write(ret.success != true);

	// associate the user with the project, and then try to dissociate
	var assignRet = get("projectApi.php", "assignUser", assignInfo);
	var dissRet = get("projectApi.php", "dissociateUsers", assignInfo);
	write(dissRet.success);
}
function testGetUsers(){
	write("Testing getUsers");
	// testing a legitimate project...make a project
	var name = { "projName" : "Test project"};
	var addRet = get("projectApi.php", "addProject", name);
      
	// test a project with no users (should return false)
	var info = {"projectId" : addRet.projectId};
	var falseRet = get("projectApi.php", "getUsers", info);
	write(falseRet.success != true);
	// assign that project a user, and test again
	var assignInfo = { "users" : [2], "projectId" : addRet.projectId };
	get("projectApi.php", "assignUser", assignInfo);
	var ret = get("projectApi.php","getUsers", info);
	write(ret.success);
}
function testListReleases(){
	write("Testing listReleases");
	//test a user NOT associated with the project
	helperLogin(true);
	var info = { "projName" : "test project" };
	var ret = get("projectApi.php", "addProject", info);
	var listInfo = { "projectId" : ret.projectId};
	var ret2 = get("projectApi.php","listReleases", listInfo);
	write(ret2.success != true);
	//testing a user that is associated with the project
	helperLogout();
	helperLogin(true);
	var assignInfo = { "users" : [2], "projectId" : ret.projectId };
	var assignRet = get("projectApi.php", "assignUser", assignInfo);
	write(assignRet.success);
	var project = { "projectId" : ret.projectId };
	var ret2 = get("projectApi.php", "listReleases", project);
	write(ret2.success);
}
function testAssignUser(){
    write("Testing assignUser");
    var ret = helperAssignUser(380);
    write(ret.success);
}
function testListProjects(){
	write("Testing listProjects");
	helperLogin(true);
	var info = {};
	var ret = get("projectApi.php","listProjects",info);
	write(ret.success);
	
}
function helperAssignUser(projectId){
	var userInfo = helperGetInfo();
	var users = [ userInfo.userId ];
	var info = {"users" : users ,
		"projectId" : projectId}
	var ret = get("projectApi.php","assignUser",info);
	return ret;
}
function helperAddProject(){
	var info = {"projName" : "Test Proj"}
	var ret = get("projectApi.php","addProject",info);
	return ret;

}
function helperDelProject(projectId){
	var info = {"projectId": projectId}
	var ret = get("projectApi.php","delProject",info);
	return ret;
}
function testAddProject(){
	write("Testing addProject");
	var ret = helperAddProject();
	if(ret.hasOwnProperty("projectId")){
		write(ret.projectId >= 0);
	}else write("false");
	var stat = helperAssignUser(ret.projectId);
	write((stat.success == true));
	var stat2 = helperDelProject(ret.projectId);
	write((stat2.success == true));
}
function testRenameProject(){
	write("Testing renameProject");
	var proj = helperAddProject();
	helperAssignUser(proj.projectId);
	var rename = "Proj Rename";
	var info = {"projectId" : proj.projectId,
			"projName" : rename}
	var ret = get("projectApi.php", "renameProject",info);
	write(ret.success == true);
	var ret = get("projectApi.php","listProjects",null);
	write((proj.projectId in ret.projects) && ret.projects[proj.projectId] == rename);
	console.log(ret);
}
function testAddProject(){
    write("Testing addProject()");
    var proj = { "projName" : "Testing Project"};
    helperLogout();
    var ret = get("projectApi.php", "addProject", proj);
    // when logged out, should not be able to add a project...
    write(ret.success != true);
    // when logged in as not admin, should not be able to add a project...
    helperLogin(false);
    var ret2 = get("projectApi.php", "addProject", proj);
    write(ret2.success != true);
    helperLogout();
    //when logged in as admin, SHOULD be able to add a project
    helperLogin(true);
    var ret3 = get("projectApi.php", "addProject", proj);
    write(ret3.success);
}
function testAddRelease(){
    write("Testing addRelease()");
    var release = {"projectId" : 1, "name" : "test release"};
    get("userApi.php", "logout", null);
    var ret = get("projectApi.php", "addRelease", release);
    // when logged out, should not be able to add a project...
    write(ret.success != true);
    helperLogin(true);
    ret = get("projectApi.php", "addRelease", release);
    write(ret.success);
}
function testDelRelease(){
    //logged in as admin, should be able to delete a release
    write("Testing delRelease");
    // add a project and give it a release, then delete that release as admin
    helperLogin(true);
    // add project
    var name = { "projName" : "releaseTest" };
    var projId =  get("projectApi.php", "addProject", name);
    // associate user with project
    var userInfo = { "users" : [2], "projectId" : projId.projectId };
    var test = get("projectApi.php", "assignUser", userInfo);

    // make a release
    var info = { "name" : "tester", "projectId" : projId.projectId};
    var rel = get("projectApi.php", "addRelease", info);
    
    // delete that release
    var relInfo = { "release_id" : rel.releaseId };
    var ret = get("projectApi.php", "delRelease", relInfo);
    // test is currently not working
    // write(ret.success);
    // logged in as non-admin, should not be able to delete a project   
    helperLogin(false);
    var id3 = { "releaseId" : 2 };
    var ret3 = get("projectApi.php", "delRelease", id3);
    write(ret3.success != true);
    // logged in as admin, should not be able to delete a project that doesnt                                                                         
     helperLogout();
     helperLogin(true);
     var id4 = { "releaseId" : 1444444 };
     var ret4 = get("projectApi.php", "delRelease", id4);
     write(ret4.success != true);
}          
function testDelProject(){
    write("Testing delProject()");
    // as admin, should be able to delete the project, not imitatible
    // TEST WORKED but I wasn't sure how to generate a legitimate
    // projectId dynamically, so this will only work the first time
    helperLogin(true);
    var name = { "projName" : "tester" };
    var tester_id = get("projectApi.php", "addProject", name);

    var ret = get("projectApi.php", "delProject", tester_id);
     write(ret.success);
    // logged out, should not be able to delete a project
    helperLogout();

    // logged in as non-admin, should not be able to delete a project
    helperLogin(false);
    tester_id = get("projectApi.php", "addProject", name);

    ret = get("projectApi.php", "delProject", tester_id);
    write(ret.success != true);
    // logged in as admin, should not be able to delete a project that doesn't exist
    helperLogout();
    helperLogin(true);
    var id4 = { "projectId" : 1444444 };
    var ret4 = get("projectApi.php", "delProject", id4);
    write(ret4.success != true);

}
function testListStickies(){
    write("Testing listStickies");
    helperLogin(true);
    // add a project, add a release, assign user to project then add stickies to it
     var projName = {"projName" : "Test project"};
      var addRet = get("projectApi.php", "addProject", projName);
    //assign user to project
     var assignInfo = {"users" : [2], "projectId" : addRet.projectId};
     var assignRet = get("projectApi.php", "assignUser", assignInfo);

    //add release to project
    var relInfo = {"projectId" : addRet.projectId, "name" : "Test release"};
    var relRet = get("projectApi.php", "addRelease", relInfo);
    

	var stickyId = helper2AddSticky(relRet.releaseId);
	

    
     var id = {"releaseId" : relRet.releaseId};
     var ret = get("projectApi.php", "listStickies", id);
      write(ret.success);

}
